package com.breeze.blockchain.auth.listener;

import cn.dev33.satoken.listener.SaTokenListenerForSimple;
import cn.dev33.satoken.stp.SaLoginModel;
import com.breeze.blockchain.auth.util.LoginLogics;
import com.breeze.blockchain.service.AdminService;
import com.breeze.blockchain.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 用户登陆相关监听器
 *
 * @author breeze
 */
@Component
@Slf4j
public class UserLoginListener extends SaTokenListenerForSimple {

  @Resource UserService userService;
  @Resource AdminService adminService;

  @Override
  public void doLogin(
      String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
    var id = Long.parseLong(String.valueOf(loginId));
    switch (loginType) {
      case LoginLogics.APP -> userService.updateLastLoginInfo(id);
      case LoginLogics.ADMIN ->
          adminService.updateLastLoginInfo(Long.parseLong(String.valueOf(loginId)));
    }
  }
}
